const assessFacade = require('../facade/assess.server.facade.js');
const _ = require('lodash');
var eventproxy = require('eventproxy');

module.exports = {
    addAssessByFoodId: addAssessByFoodId,
    getAllAssessByFoodItemId: getAllAssessByFoodItemId,
    getAssessWithoutRelayByMerchantId: getAssessWithoutRelayByMerchantId,
    getAssessWithRelayByMerchantId: getAssessWithRelayByMerchantId,
    replyAssessByOrderId:replyAssessByOrderId
}

var ep = new eventproxy();

ep.on('err', function err(res, status, errMessage) {
    var data = {
        code: status,
        message: errMessage,
        data: null
    }
    res.json(data);
});

function addAssessByFoodId(req, res, next) {

    let body = req.body;
    let id = body.foodId;
    let assess = body.assess;


    assessFacade.addAssessByFoodId(id, assess, function addAssessByFoodIdCB(error, food) {

        if (error) {
            return next(error);
        }
        var data = {
            code: 200,
            message: 'Add success',
            data: null
        }
        res.json(data);

    });

}

function replyAssessByOrderId(req, res, next) {

    let body = req.body;
    let id = body.orderId;
    let reply = body.reply;


    assessFacade.updateAssessByOrderId(id, reply, function updateAssessByOrderIdCB(error, food) {

        if (error) {
            return next(error);
        }
        var data = {
            code: 200,
            message: 'Reply success',
            data: null
        }
        res.json(data);

    });

}


function getAllAssessByFoodItemId(req, res, next) {

    let params = req.params;
    let id = params.foodId;

    assessFacade.getAllAssessByFoodItemId(id,function addAssessByFoodIdCB(error, result) {

        if (error) {
            return next(error);
        }
        var data = {
            code: 200,
            message: 'Get success',
            data: result
        }
        res.json(data);

    });

}


function getAssessWithoutRelayByMerchantId(req, res, next) {

    let params = req.params;
    let id = params.merchantId;

    assessFacade.getAssessWithoutRelayByMerchantId(id,function addAssessByFoodIdCB(error, result) {

        if (error) {
            return next(error);
        }
        var data = {
            code: 200,
            message: 'Get success',
            data: result
        }
        res.json(data);

    });

}

function getAssessWithRelayByMerchantId(req, res, next) {

    let params = req.params;
    let id = params.merchantId;

    assessFacade.getAssessWithRelayByMerchantId(id,function addAssessByFoodIdCB(error, result) {

        if (error) {
            return next(error);
        }
        var data = {
            code: 200,
            message: 'Get success',
            data: result
        }
        res.json(data);

    });

}

